package com.cp.order.platform;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;

public abstract class BaseController extends Controller {

	protected Map<String, String> getAppParameter() {
		String data = getRequest().getParameter("data");
		if (StrKit.isBlank(data)) {
			return new HashMap<>();
		} else {
			Map<String, String> map = JSON.parseObject(data, new TypeReference<Map<String, String>>() {
			});
			return map;
		}
	}

	protected String getResult(int status, String msg, Object data) {
		return JsonKit.toJson(new ResultData(status, msg, data));
		// return JSON.toJSONString(new ResultData(status, msg, data));
	}

	protected String getResult(int status, String msg) {
		return JsonKit.toJson(new ResultData(status, msg));
		// return JSON.toJSONString(new ResultData(status, msg));
	}

	/**
	 * 正常返回
	 */
	protected void returnOK(Object data) {
		renderJson(getResult(ResultData.STATUS_OK, "成功", data));
	}

	protected void returnOK() {
		renderJson(getResult(ResultData.STATUS_OK, "成功", null));
	}

	/**
	 * 服务器异常
	 */
	protected void returnServerError(String msg) {
		renderJson(getResult(ResultData.STATUS_SERVER_ERROR, msg));
	}

	/**
	 * 业务错误
	 */
	protected void returnBusinessError(String msg) {
		renderJson(getResult(ResultData.STATUS_BUSINESS_ERROR, msg));
	}

	/**
	 * 自定义状态码错误
	 */
	protected void returnCustomerError(int status, String msg) {
		renderJson(getResult(status, msg));
	}

}
